package com.zablert.mainactivity.main

import android.content.res.Configuration
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.Image
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.selection.selectable
import androidx.compose.foundation.selection.selectableGroup
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.alibaba.android.arouter.facade.annotation.Route
import com.zablert.mainactivity.BaseRouteConstants.CHAT_ACTIVITY_PATH
import com.zablert.mainactivity.BaseRouteConstants.ITEM_ANIMATOR_PATH
import com.zablert.mainactivity.BaseRouteConstants.MAIN_PATH
import com.zablert.mainactivity.BaseRouteConstants.NORMAL_ACTIVITY_PATH
import com.zablert.mainactivity.BaseRouteConstants.TEACHING_ACTIVITY_PATH
import com.zablert.mainactivity.R
import com.zablert.mainactivity.ui.theme.MainTheme
import zyc.zalbert.router.RouterUtils


@Route(path = MAIN_PATH)
class MainActivity : ComponentActivity() {
    val messages = listOf(
        FuckingParam(
            "RecyclerView",
            "RecyclerView真的很重要，你可以把它当做一个滚动的容器，把每一个item都放在里面，然后滚动它就可以了。",
            ITEM_ANIMATOR_PATH
        ),
        FuckingParam(
            "ChatView",
            "多布局方案，气泡方案等",
            CHAT_ACTIVITY_PATH
        ),
        FuckingParam(
            "TeachingView",
            "用于公司教学使用的视图",
            TEACHING_ACTIVITY_PATH
        ),
        FuckingParam(
            "NormalView",
            "用来研究嵌套滑动的视图",
            NORMAL_ACTIVITY_PATH
        ),
        FuckingParam(
            "SnackBarView",
            "用来研究SnackBar的视图",
            NORMAL_ACTIVITY_PATH
        ),
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MainTheme {
                FuckingList(messages)
            }
        }
    }

    @Composable
    fun FuckingCard(fuking: FuckingParam, isSelect: Boolean, click: () -> Unit) {
        val surfaceColor by animateColorAsState(
            targetValue = if (isSelect) {
                Color(0xFFCCCCCC)
            } else {
                MaterialTheme.colors.surface
            }
        )
        Surface(
            shape = MaterialTheme.shapes.medium,
            elevation = 5.dp,
            color = surfaceColor,
            modifier = Modifier
                .padding(horizontal = 8.dp, vertical = 5.dp)
//                .clickable(onClick = onMClick)
                .selectable(
                    selected = isSelect,
                    onClick = click
                )
                .selectableGroup()
//                .combinedClickable(onLongClick = {},onClick = {})
        ) {
            Row(
                modifier = Modifier
                    .padding(horizontal = 15.dp, vertical = 10.dp)
                    .fillMaxSize()
            ) {
                Image(
                    painterResource(id = R.mipmap.ic_launcher_round),
                    contentDescription = "",
                    modifier = Modifier
                        .size(50.dp)
                        .border(1.5.dp, MaterialTheme.colors.secondary, shape = CircleShape)
                )
                Spacer(modifier = Modifier.padding(horizontal = 5.dp))
                Column(
                    modifier = Modifier
                        .fillMaxHeight()
                        .weight(1F, true)
                ) {
                    Text(
                        text = fuking.title,
                        color = MaterialTheme.colors.secondaryVariant,
                        style = MaterialTheme.typography.subtitle2,
                    )
                    Spacer(modifier = Modifier.padding(vertical = 4.dp))
                    Text(
                        text = fuking.description,
                        style = MaterialTheme.typography.body2,
                        overflow = TextOverflow.Ellipsis,
                        maxLines = if (isSelect) Int.MAX_VALUE else 1,
                        modifier = Modifier.animateContentSize()
                    )
                }
                Image(
                    painterResource(id = R.mipmap.icon_arrow_right),
                    contentDescription = "",
                    contentScale = ContentScale.Fit,
                    modifier = Modifier
                        .padding(start = 5.dp)
                        .align(
                            Alignment.CenterVertically
                        )
                        .clickable {
                            RouterUtils.INSTANCE.goto(fuking.path)
                        }
                )
            }
        }
    }

    @Composable
    fun FuckingList(fakings: List<FuckingParam>) {
        var selectedTag by remember { mutableStateOf<FuckingParam?>(null) }
        LazyColumn {
            itemsIndexed(fakings) { index, faking ->
                val isSelect = selectedTag == faking
                FuckingCard(faking, isSelect) {
                    selectedTag = if (isSelect) {
                        null
                    } else {
                        faking
                    }
                }

            }
        }
    }

    @Preview(name = "Light Mode")
    @Preview(
        uiMode = Configuration.UI_MODE_NIGHT_YES,
        showBackground = true,
        name = "Dark Mode"
    )
    @Preview
    @Composable
    fun DefaultPreview() {
        MainTheme {
            FuckingList(messages)
        }
    }
}
